package com.imooc.utils;

import java.sql.*;

/**
 * Created by zghgchao 2017/12/26 11:10
 * 操作MySQL的工具类
 */
public class MySQLUtils {

    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private static final String DRIVERCLASS = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://172.17.66.51:3306/imooc_project";

    /**
     * 数据库连接
     *
     * @return
     */
    public static Connection getConnection() {
        Connection connection = null;

        try {
            Class.forName(DRIVERCLASS);

            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 释放资源
     *
     * @param connection
     * @param patmt
     * @param rs
     */
    public static void release(Connection connection, PreparedStatement patmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (patmt != null) {
            try {
                patmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(getConnection());
    }
}
